package edu.yourl.web;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.validation.EqualInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator;
import org.apache.wicket.validation.validator.EmailAddressValidator;

import edu.yourl.application.YourlSession;
import edu.yourl.domain.exception.NicknameAlreadyRegisteredException;
import edu.yourl.presenter.IRegistrationPresenter;
import edu.yourl.web.components.PropertyTextField;

public class RegistrationPage extends WebPage implements IRegistrationPage {
	
	String name;
	String nickname;
	String password;
	String repassword;
	String email;
	
	@SpringBean
	IRegistrationPresenter presenter;
	
	public RegistrationPage() {
		
		add(new RegistrationForm("registrationForm"));
		add(new FeedbackPanel("feedbackPanel"));
		
	}
	
	public String getName() {
		return name;
	}


	public String getNickname() {
		return nickname;
	}


	public String getPassword() {
		return password;
	}

	public String getRepassword() {
		return repassword;
	}

	public String getEmail() {
		return email;
	}
	
	
	private class RegistrationForm extends Form<String> {
		
		private static final long serialVersionUID = 1L;
				
		public RegistrationForm(String id) {
			super(id);
			this.setMultiPart(true);
			PasswordTextField password = new PasswordTextField("password", new PropertyModel<String>(RegistrationPage.this, "password"));
			this.add(new PropertyTextField<String>("name",RegistrationPage.this).setRequired(true));
			PropertyTextField<String> nicknameField = new PropertyTextField<String>("nickname",RegistrationPage.this);;
			nicknameField.setRequired(true);
			nicknameField.add(new NoWhiteSpacesValidator());
			this.add(nicknameField);
			PasswordTextField  repassword = new PasswordTextField("repassword",new PropertyModel<String>(RegistrationPage.this, "password"));
			this.add(password);
			this.add(repassword);
			this.add(new PropertyTextField<String>("email",RegistrationPage.this).setRequired(true).add(EmailAddressValidator.getInstance()));
			
			this.add(new EqualInputValidator(password, repassword));
			
		}
		
		@Override
		protected void onSubmit() {
			try {
				RegistrationPage.this.presenter.registerUser(RegistrationPage.this);
				this.setResponsePage(SuccessfulRegistration.class);
			} catch (NicknameAlreadyRegisteredException e) {
				error("Ya hay otro usuario con el mismo sobrenombre.");
			}
		}
		
		
	}
	
	public YourlSession getYourlSession() {
		return (YourlSession) super.getSession(); 
	}
	
	public void setPresenter(IRegistrationPresenter presenter) {
		this.presenter = presenter;
	}

	@SuppressWarnings("serial")
	private class NoWhiteSpacesValidator extends AbstractValidator<String> {
		
		private static final String NO_WHITE_SPACES = "no.white.spaces";

		@Override
		protected void onValidate(IValidatable<String> validatable) {
			String content = validatable.getValue();
			if (content.contains(" ")) {
				error(validatable,NO_WHITE_SPACES);
			}
		}
	}
}
